#include "U2LogManager.h"

#include "U2Exception.h"


U2EG_NAMESPACE_USING


//-----------------------------------------------------------------------
template<> U2LogManager* U2Singleton<U2LogManager>::s_pSingleton = 0;

U2LogManager* U2LogManager::getSingletonPtr(void)
{
    return s_pSingleton;
}

U2LogManager& U2LogManager::getSingleton(void)
{  
    assert( s_pSingleton );
    return ( *s_pSingleton );  
}
//-----------------------------------------------------------------------
U2LogManager::U2LogManager()
{
	m_pDefaultLog = NULL;
}
//-----------------------------------------------------------------------
U2LogManager::~U2LogManager()
{
	U2_LOCK_AUTO_MUTEX
	// Destroy all logs
	LogList::iterator i;
	for (i = m_logs.begin(); i != m_logs.end(); ++i)
	{
		U2_DELETE(i->second);
	}
}
//-----------------------------------------------------------------------
U2Log* U2LogManager::createLog( const U2String& name, bool defaultLog, bool debuggerOutput, 
						   bool suppressFileOutput)
{
	U2_LOCK_AUTO_MUTEX

	U2Log* newLog = U2_NEW U2Log(name, debuggerOutput, suppressFileOutput);

	if( !m_pDefaultLog || defaultLog )
	{
		m_pDefaultLog = newLog;
	}

	m_logs.insert( LogList::value_type( name, newLog ) );

	return newLog;
}
//-----------------------------------------------------------------------
U2Log* U2LogManager::getDefaultLog()
{
	U2_LOCK_AUTO_MUTEX
	return m_pDefaultLog;
}
//-----------------------------------------------------------------------
U2Log* U2LogManager::setDefaultLog(U2Log* newLog)
{
	U2_LOCK_AUTO_MUTEX
	U2Log* oldLog = m_pDefaultLog;
	m_pDefaultLog = newLog;
	return oldLog;
}
//-----------------------------------------------------------------------
U2Log* U2LogManager::getLog( const U2String& name)
{
	U2_LOCK_AUTO_MUTEX
		LogList::iterator i = m_logs.find(name);
	if (i != m_logs.end())
		return i->second;
	else
		U2_EXCEPT(U2Exception::ERR_INVALIDPARAMS, "Log not found. ", "U2LogManager::getLog");
}
//-----------------------------------------------------------------------
void U2LogManager::destroyLog(const U2String& name)
{
	LogList::iterator i = m_logs.find(name);
	if (i != m_logs.end())
	{
		if (m_pDefaultLog == i->second)
		{
			m_pDefaultLog = 0;
		}
		U2_DELETE(i->second);
		m_logs.erase(i);
	}

	// Set another default log if this one removed
	if (!m_pDefaultLog && !m_logs.empty())
	{
		m_pDefaultLog = m_logs.begin()->second;
	}
}
//-----------------------------------------------------------------------
void U2LogManager::destroyLog(U2Log* log)
{
	destroyLog(log->getName());
}
//-----------------------------------------------------------------------
void U2LogManager::logMessage( const U2String& message, LogMessageLevel lml, bool maskDebug)
{
	U2_LOCK_AUTO_MUTEX
	if (m_pDefaultLog)
	{
		m_pDefaultLog->logMessage(message, lml, maskDebug);
	}
}
//-----------------------------------------------------------------------
void U2LogManager::setLogDetail(LoggingLevel ll)
{
	U2_LOCK_AUTO_MUTEX
	if (m_pDefaultLog)
	{
		m_pDefaultLog->setLogDetail(ll);
	}
}
//---------------------------------------------------------------------
U2Log::Stream U2LogManager::stream(LogMessageLevel lml, bool maskDebug)
{
	U2_LOCK_AUTO_MUTEX
	if (m_pDefaultLog)
		return m_pDefaultLog->stream(lml, maskDebug);
	else
		U2_EXCEPT(U2Exception::ERR_INVALIDPARAMS, "Default log not found. ", "U2LogManager::stream");
}